﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EzyGoEzySystem.Interfaces;
using EzyGoEzySystem.Models;

namespace EzyGoEzySystem.Services
{
    /// <summary>
    /// implement IBoardAlightService
    /// </summary>
    /// <author>Tran Binh Thanh</author>
    public class BoardAlightService : IBoardAlightService
    {
        public IEnumerable<TravelJournalEntry> BoardBus(IEnumerable<Account> listAccounts, DateTime boardTime, EzyGoEzyDBContext dbContext)
        {
            //create a list of TravelJournalEntry
            List<TravelJournalEntry> travelList = new List<TravelJournalEntry>();

            //new database context
            //var dbContext = new EzyGoEzyDBContext();

            foreach (var account in listAccounts)
            {
                //TravelJournalEntry will add in to database
                var travelEntry = new TravelJournalEntry
                {
                    BoardTime = boardTime,
                    AlightTime = null,
                    Fare = 0, //fare is computed later
                    AccountID = account.ID,
                    TravelStatus = "Pending"
                    //Account = account
                };

                //add this entry to TravelJournalEntry database
                dbContext.TravelJournalEntries.Add(travelEntry);
                dbContext.SaveChanges();

                //also add this travelEntry to TravelJournalEntry to return
                travelList.Add(travelEntry);
            }

            // cleanup dbContext
            //dbContext.Dispose();

            //return list of TravelJournalEntry
            return travelList;
        }


        public IEnumerable<TravelJournalEntry> AlightBus(IEnumerable<Account> listAccounts, DateTime alightTime, EzyGoEzyDBContext dbContext)
        {
            //create a list of TravelJournalEntry
            List<TravelJournalEntry> travelList = new List<TravelJournalEntry>();

            //new database context
            //var dbContext = new EzyGoEzyDBContext();

            foreach (var account in listAccounts)
            {
                //find an TravelEntryID of this passenger
                var travelID = from travelEntry in dbContext.TravelJournalEntries
                               where travelEntry.Account.ID == account.ID
                               && travelEntry.AlightTime == null
                               select travelEntry;
                var entry = travelID.First(); //this is an entry we will update

                //update AlightTime entry for this passenger
                entry.AlightTime = alightTime;
                entry.TravelStatus = "Completed";

                //add this entry to TravelJournalEntry list
                travelList.Add(entry);

                dbContext.SaveChanges();
            }

            // cleanup dbContext
            // dbContext.Dispose();

            //return list of TravelJournalEntry
            return travelList;
        }
    }
}